# -*- coding: utf-8 -*-
import accum_band_mean
import hourly_band_mean
import matplotlib.pyplot as plt
import numpy as np
import copy


def plot_hourly_accum(event, hourly_band, hourly_gauges, accum_band,
                      accum_gauges):
    plt.style.use('fivethirtyeight')
    # plot uncertainty band and gauges value
    labels = ('band', 'gague', 'average')
    indexes = hourly_gauges.index
    time_label = [str(hour) for hour in indexes.hour]
    # time_label = [str(day) + "_" + str(hour) for day, hour
    #               in zip(indexes.day, indexes.hour)]
    x_series = np.arange(len(time_label))
    fig, ax = plt.subplots(nrows=2, ncols=1,
                           sharex=True, figsize=(15, 10))
    # plot hourly precipitation
    ax[0].fill_between(x_series, hourly_band[0], hourly_band[1],
                       color='#539caf', alpha=0.4, label=labels[0])
    ax[0].plot(x_series, hourly_gauges, color='#FF7F0E',
               lw=2, alpha=1, label=labels[1])
    ax[0].plot(x_series, np.mean(hourly_band, axis=0), color='#539caf',
               lw=2, alpha=1, label=labels[2])
    ax[0].yaxis.set_tick_params(labelsize=20)
    ax[0].set_title(event, fontsize=28)
    ax[0].set_yticks(np.arange(0, max(max(hourly_band[1]), max(hourly_gauges))
                               + 1, 2))
    # ax[0].legend(loc='upper left', fontsize='xx-large')

    # plot accumulated precipitation
    ax[1].fill_between(x_series, accum_band[0], accum_band[1],
                       color='#539caf', alpha=0.4, label=labels[0])
    ax[1].plot(x_series, accum_gauges, color='#FF7F0E',
               lw=2, alpha=1, label=labels[1])
    ax[1].plot(x_series, np.mean(accum_band, axis=0), color='#539caf',
               lw=2, alpha=1, label=labels[2])
    ax[1].set_xlabel('time (h)', fontsize=22)
    ax[1].set_ylabel('precipitation (mm)', fontsize=22)
    ax[1].xaxis.set_tick_params(labelsize=20)
    ax[1].yaxis.set_tick_params(labelsize=20)
    ax[1].set_yticks(np.arange(0, max(max(accum_band[1]), max(accum_gauges))
                               + 1, 20))
    plt.tight_layout()
    plt.show()


def main(path, domain, start_time, end_time, event):
    """plot the hourly and accumulated uncertainty band and gauges value"""
    # get uncertainty band and center value of gauges
    # hourly_band, hourly_center = hourly_band_center.get_hourly_band_center(
    #     path, domain, start_time, end_time)
    # accum_band, accum_center = accum_band_center.get_accum_band_center(
    #     path, domain, start_time, end_time)
    # plot_hourly_accum(event, hourly_band, hourly_center, accum_band,
    #                   accum_center)

    # get uncertainty band and mean value of gauges
    hourly_band, hourly_mean = hourly_band_mean.get_hourly_band_mean(
        path, domain, start_time, end_time)

    accum_band, accum_mean = accum_band_mean.get_accum_band_mean(
        path, domain, start_time, end_time)

    plot_hourly_accum(event, hourly_band, hourly_mean, accum_band,
                      accum_mean)


if __name__ == '__main__':
    # new run
    # R1
    path = r'H:/research/rainfall_estimation/wrf_output/2008/01/1700/103090'
    domain = r'03'
    startTime = r'2008-01-17_00_00_00'
    endTime = r'2008-01-19_12_00_00'
    event = 'R1'

    # R2
    # path = r'F:/Research/rainfall_estimation/WRF_Output/2008/01/1912/103090/'
    # domain = r'03'
    # startTime = r'2008-01-19_12_00_00'
    # endTime = r'2008-01-22_00_00_00'
    # event = 'R2'

    # R3
    # path = r'F:/Research/rainfall_estimation/WRF_Output/2008/03/1500/103090/'
    # domain = r'03'
    # startTime = r'2008-03-15_00_00_00'
    # endTime = r'2008-03-16_12_00_00'
    # event = 'R3'

    # R4
    # path = r'F:/Research/rainfall_estimation/WRF_Output/2008/08/1718/103090/'
    # domain = r'03'
    # startTime = r'2008-08-17_18_00_00'
    # endTime = r'2008-08-20_00_00_00'
    # event = 'R3'

    # R5
    # path = r'F:/Research/rainfall_estimation/WRF_Output/2008/09/2900/139/'
    # domain = r'03'
    # startTime = r'2008-09-29_00_00_00'
    # endTime = r'2008-10-02_06_00_00'
    # event = 'R4'

    # R6
    # path = r'F:/Research/rainfall_estimation/WRF_Output/2008/12/0318/139/'
    # domain = r'03'
    # startTime = r'2008-12-03_18_00_00'
    # endTime = r'2008-12-06_00_00_00'
    # event = 'R6'

    # R7
    # path = r'F:/Research/rainfall_estimation/WRF_Output/2008/12/1200/139/'
    # domain = r'03'
    # startTime = r'2008-12-12_00_00_00'
    # endTime = r'2008-12-14_06_00_00'
    # event = 'R7'

    main(copy.deepcopy(path), copy.deepcopy(domain), copy.deepcopy(startTime),
         copy.deepcopy(endTime), copy.deepcopy(event))
